﻿using System.Collections.Generic;
using System.Domain.Entity;
using System.Domain.IRepository;
using System.Linq;
using System.Repository.Base;
using System.Repository.DataBase.Models;
using Domain.Infrastructure;
using Microsoft.EntityFrameworkCore;

namespace System.Repository
{
    //class SysRoleRepository
    public class SysRoleRepository : RepositoryDomain<SysRoleEntity, Sys_Role>, ISysRoleRepository
    {
        public SysRoleRepository(IDbContextConfig config) : base(config)
        {
        }
        public override bool Update(SysRoleEntity entity)
        {
            Sys_Role model = GetModel(entity.Id);
            if (model == null)
                return false;
            Dbcontext.Set<Sys_Role>().Attach(model);
            Dbcontext.Entry(model).State = EntityState.Unchanged;
            AutoCopy(model, entity);
            return Dbcontext.SaveChanges() > 0;
        }



        public override bool Delete(string key)
        {
            // 事务
            var temp = Transaction(() =>
            {
                var entitys1 = Dbcontext.Set<Sys_RoleAuthorize>().Where(t => t.RoleId == key).ToList();
                entitys1.ForEach(m => Dbcontext.Entry(m).State = EntityState.Deleted);
                //删除 角色
                var entitys = Dbcontext.Set<Sys_Role>().Where(t => t.Id == key).ToList();
                entitys.ForEach(m => Dbcontext.Entry(m).State = EntityState.Deleted);
                return Dbcontext.SaveChanges() > 0;

            });

            return temp;
            //return Delete(t => t.Id == (string)key) > 0;
        }


        public bool Insert(SysRoleEntity roleEntity, string[] permissionIds)
        { 
            SysModuleRepository module = new SysModuleRepository(ContextConfig);
            SysModuleAuthorizeRepository moduleAuthorize = new SysModuleAuthorizeRepository(ContextConfig);
            List<Sys_RoleAuthorize> permissionS = new List<Sys_RoleAuthorize>();
            foreach (var item in permissionIds)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    Sys_RoleAuthorize authorizeModel = new Sys_RoleAuthorize { RoleId = roleEntity.Id, Id = Common.GuId(), CreatorTime = DateTime.Now };
                    if (module.GetModel(item) != null)
                    {
                        authorizeModel.ModuleId = item;
                        authorizeModel.Type = 1;
                    }
                    else if (moduleAuthorize.GetModel(item) != null)
                    {
                        authorizeModel.AuthorizeId = item;
                        authorizeModel.Type = 2;
                    }
                    permissionS.Add(authorizeModel);
                }
            }
            Sys_Role model = AutoCopy(null, roleEntity);
         
            // 事务
            var temp = Transaction(() =>
            {
                Dbcontext.Entry(model).State = EntityState.Added;
                foreach (var item in permissionS)
                {
                    Dbcontext.Entry(item).State = EntityState.Added;
                }
                return Dbcontext.SaveChanges() > 0;

            });

            return temp ;
        }

        public bool Update(SysRoleEntity roleEntity, string[] permissionIds)
        { 
            SysModuleRepository module = new SysModuleRepository(ContextConfig);
            SysModuleAuthorizeRepository moduleAuthorize = new SysModuleAuthorizeRepository(ContextConfig);
            List<Sys_RoleAuthorize> permissionS = new List<Sys_RoleAuthorize>();
            foreach (var item in permissionIds)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    Sys_RoleAuthorize authorizeModel = new Sys_RoleAuthorize
                    {
                        RoleId = roleEntity.Id,
                        Id = Common.GuId(),
                        CreatorTime = DateTime.Now
                    };
                    if (module.GetModel(item) != null)
                    {
                        authorizeModel.ModuleId = item;
                        authorizeModel.Type = 1;
                    }
                    else if (moduleAuthorize.GetModel(item) != null)
                    {
                        authorizeModel.AuthorizeId = item;
                        authorizeModel.Type = 2;
                    }
                    permissionS.Add(authorizeModel);
                }

            }
            Sys_Role model = GetModel(roleEntity.Id);
            if (model == null)
                return false;
            //Dbcontext.Set<Sys_Role>().Attach(model);
            //model= AutoCopy(null, roleEntity);
            Dbcontext.Set<Sys_Role>().Attach(model);
            Dbcontext.Entry(model).State = EntityState.Unchanged;
            AutoCopy(model, roleEntity); 

            // 事务
            var temp = Transaction(() =>
            {
                var entitys = Dbcontext.Set<Sys_RoleAuthorize>().Where(t => t.RoleId == roleEntity.Id).ToList();
                entitys.ForEach(m => Dbcontext.Entry(m).State = EntityState.Deleted);
                foreach (var item in permissionS)
                {
                    Dbcontext.Entry(item).State = EntityState.Added;
                }
                return Dbcontext.SaveChanges() > 0;

            });

            return temp; 
        }




    }
}
